package com.syezon.wifikey.redbag.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.ad.AdPositionEnum;
import com.syezon.wifikey.analysis.AnalysisHelper;
import com.syezon.wifikey.imageloader.BannerImageLoader;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.redbag.info.RedBagInfo;
import com.syezon.wifikey.web.WebHelper;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.DefaultTransformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/13.
 */

public class RedBagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<RedBagInfo> mData = new ArrayList<>();
    private List<AdInfo> mCurrentTopAdList;

    public RedBagAdapter(Context context) {
        this.mContext = context;
    }

    public void setDate(List<RedBagInfo> date) {
        if (date != null) {
            mData.clear();
            mData.addAll(date);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == RedBagInfo.TYPE_TOP_BANNER) {
            return new TopBannerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_red_bag_top_banenr, parent, false));
        }
        if (viewType == RedBagInfo.TYPE_RED_BAG_DIVIDER) {
            return new RedBagDividerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_red_bag_diveder, parent, false));
        }

        if (viewType == RedBagInfo.TYPE_RED_BAG_ITEM) {
            return new RedBagViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_red_bag_item, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RedBagInfo redBagInfo = mData.get(position);
        int type = redBagInfo.getType();
        if (type == RedBagInfo.TYPE_TOP_BANNER) {
            TopBannerViewHolder viewHolder = (TopBannerViewHolder) holder;
            final List<AdInfo> topBaner = redBagInfo.getTopBaner();
            if (topBaner != mCurrentTopAdList) {
                if (topBaner != null && topBaner.size() > 0) {
                    List<String> picList = new ArrayList<>();
                    for (AdInfo adInfo : topBaner) {
                        picList.add(adInfo.getPic());
                    }
                    viewHolder.banner.setImages(picList)
                            .setImageLoader(new BannerImageLoader())
                            .setBannerAnimation(DefaultTransformer.class)
                            .setDelayTime(3000)
                            .setBannerStyle(BannerConfig.NOT_INDICATOR)
                            .start();
                    viewHolder.banner.setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(int position) {
                            AdInfo adInfo = topBaner.get(position);
                            WebHelper.showAd(mContext, adInfo.getUrl(), adInfo.getName());
                        }
                    });
                    mCurrentTopAdList = topBaner;
                }
            }

        }
        if (type == RedBagInfo.TYPE_RED_BAG_DIVIDER) {
            RedBagDividerViewHolder viewHolder = (RedBagDividerViewHolder) holder;
        }
        if (type == RedBagInfo.TYPE_RED_BAG_ITEM) {
            final AdInfo adInfo = redBagInfo.getAdInfo();
            RedBagViewHolder viewHolder = (RedBagViewHolder) holder;
            ImageLoader.getInstance().displayImage(mContext, adInfo.getPic(), viewHolder.ivAdImg);
            viewHolder.ivAdImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WebHelper.showAd(mContext, adInfo.getUrl(), adInfo.getName());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        RedBagInfo redBagInfo = mData.get(position);
        return redBagInfo.getType();
    }

    public static class TopBannerViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.banner)
        Banner banner;

        public TopBannerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class RedBagDividerViewHolder extends RecyclerView.ViewHolder {

        public RedBagDividerViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class RedBagViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_redbag_adimg)
        ImageView ivAdImg;

        public RedBagViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
